package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.*;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_BillService;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.util.ISSConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("ad_billService")
public class Ad_BillServiceImp extends GenericService implements Ad_BillService {
    @Autowired
    @Qualifier("hoaDon")
    private HoaDonDao hoaDonDao;

    @Autowired
    @Qualifier("hoaDonFull")
    private HoaDonFullDao hoaDonFullDao;

    @Autowired
    @Qualifier("chiTietHoaDon")
    private ChiTietHoaDonDao chiTietHoaDonDao;

    @Autowired
    @Qualifier("thongTinThanhVien")
    private ThongTinThanhVienDao thongTinThanhVienDao;

    @Autowired
    @Qualifier("chiTietSanPham")
    private ChiTietSanPhamDao chiTietSanPhamDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminBill(HoaDonDto billDto) {
        HoaDon hoaDon = hoaDonDao.findById(billDto.getMaHD());
        if(hoaDon == null){
            hoaDon = new HoaDon();
            hoaDon.setMaHD(billDto.getMaHD());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(billDto.getThongTinThanhVien().getMaTV());
            hoaDon.setThongTinThanhVien(thongTinThanhVien);
            hoaDon.setHoTenNN(billDto.getHoTenNN());
            hoaDon.setDiaChiNN(billDto.getDiaChiNN());
            hoaDon.setSoDTNN(billDto.getSoDTNN());
            hoaDon.setNgayNhan(billDto.getNgayNhan());
            hoaDon.setNgayGiao(billDto.getNgayGiao());
            hoaDon.setNgayLapHD(billDto.getNgayLapHD());
            hoaDon.setTrangThai(billDto.getTrangThai());
            hoaDon.setThanhToan(billDto.getThanhToan());
            hoaDon.setGhiChu(billDto.getGhiChu());
            hoaDon.setTongTien(billDto.getTongTien());
            hoaDonDao.insert(hoaDon);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminEditBill(HoaDonDto billDto) {
        HoaDon hoaDon = hoaDonDao.findById(billDto.getMaHD());
        if(hoaDon != null){
            hoaDon = new HoaDon();
            hoaDon.setMaHD(billDto.getMaHD());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(billDto.getThongTinThanhVien().getMaTV());
            hoaDon.setThongTinThanhVien(thongTinThanhVien);
            hoaDon.setHoTenNN(billDto.getHoTenNN());
            hoaDon.setDiaChiNN(billDto.getDiaChiNN());
            hoaDon.setSoDTNN(billDto.getSoDTNN());
            hoaDon.setNgayNhan(billDto.getNgayNhan());
            hoaDon.setNgayGiao(billDto.getNgayGiao());
            hoaDon.setNgayLapHD(billDto.getNgayLapHD());
            hoaDon.setTrangThai(billDto.getTrangThai());
            hoaDon.setThanhToan(billDto.getThanhToan());
            hoaDon.setGhiChu(billDto.getGhiChu());
            hoaDon.setTongTien(billDto.getTongTien());
            hoaDonDao.update(hoaDon);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<HoaDonFullDto> getBillCommon () {
        List<HoaDonFull> hoaDonFulls = hoaDonDao.getBillCommon();
        return mapper.mapCollection(hoaDonFulls, HoaDonFullDto.class);
    }

    public List<ChiTietHoaDonDto> getBillDetailCommon () {
        return mapper.mapCollection(chiTietHoaDonDao.getBillDetailCommon(), ChiTietHoaDonDto.class);
    }

    public List<ThongTinThanhVienDto> getMemberInfor () {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    public List<ChiTietSanPhamDto> getAllProductDetailCodeInBill () {
        return mapper.mapCollection(chiTietSanPhamDao.getAllProductDetail(), ChiTietSanPhamDto.class);
    }

    public HoaDonDto getBillInfoEdit(HoaDonDto billDto){
        HoaDon hoaDon = hoaDonDao.findById(billDto.getMaHD());
        HoaDonDto hoaDonDto = mapper.map(hoaDon, HoaDonDto.class,HOA_DON_MAP_ID);
        return  hoaDonDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public HoaDonDto registerBillEdit(HoaDonDto billDto) {
        HoaDon hoaDon = hoaDonDao.findById(billDto.getMaHD());
        try {
            hoaDon.setDiaChiNN(billDto.getDiaChiNN());
            hoaDon.setTongTien(billDto.getTongTien());
            hoaDon.setGhiChu(billDto.getGhiChu());
            hoaDon.setHoTenNN(billDto.getHoTenNN());
            hoaDon.setTrangThai(billDto.getTrangThai());
            hoaDon.setThanhToan(billDto.getThanhToan());
            hoaDon.setMaHD(billDto.getMaHD());
            hoaDon.setNgayNhan(billDto.getNgayNhan());
            hoaDon.setNgayGiao(billDto.getNgayGiao());
            hoaDon.setNgayLapHD(billDto.getNgayLapHD());
            hoaDon.setSoDTNN(billDto.getSoDTNN());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(billDto.getThongTinThanhVien().getMaTV());
            hoaDon.setThongTinThanhVien(thongTinThanhVien);
            hoaDonDao.update(hoaDon);
            return billDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public HoaDonDto updateStatusBillCommon(HoaDonDto billDto) {
        HoaDon hoaDon = hoaDonDao.findById(billDto.getMaHD());
        try {
//            hoaDon.setDiaChiNN(billDto.getDiaChiNN());
//            hoaDon.setTongTien(billDto.getTongTien());
//            hoaDon.setGhiChu(billDto.getGhiChu());
//            hoaDon.setHoTenNN(billDto.getHoTenNN());
            hoaDon.setTrangThai(billDto.getTrangThai());
            hoaDon.setThanhToan(billDto.getThanhToan());
            hoaDon.setMaHD(billDto.getMaHD());
//            hoaDon.setNgayDat(billDto.getNgayDat());
//            hoaDon.setNgayGiao(billDto.getNgayGiao());
//            hoaDon.setNgayLapHD(billDto.getNgayLapHD());
//            hoaDon.setSoDTNN(billDto.getSoDTNN());
//            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(billDto.getThongTinThanhVien().getMaTV());
//            hoaDon.setThongTinThanhVien(thongTinThanhVien);
            hoaDonDao.update(hoaDon);
            return billDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    public List<ThongTinThanhVienDto> getAllMemberInBill () {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    public HoaDonDto getDataId(HoaDonDto billDto){
        HoaDon hoaDon = hoaDonDao.findById(billDto.getMaHD());
        HoaDonDto hoaDonDto = mapper.map(hoaDon, HoaDonDto.class,HOA_DON_MAP_ID);
        return  hoaDonDto;
    }

    public ThongTinThanhVienDto getMemberId(ThongTinThanhVienDto memberDto){
        ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(memberDto.getMaTV());
        ThongTinThanhVienDto thongTinThanhVienDto = mapper.map(thongTinThanhVien, ThongTinThanhVienDto.class,THONG_TIN_THANH_VIEN_MAP_ID);
        return  thongTinThanhVienDto;
    }

/*    @Override
    public List<HoaDonDto> updateStatusBillCommon(String maHD, String finish, String status) {
        List<HoaDonDto> hoaDonDto = new ArrayList<HoaDonDto>();
        List<HoaDon> hoaDon = hoaDonDao.updateStatusBillCommon(maHD, finish, status);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonDto.class,HOA_DON_MAP_ID);
        return hoaDonDto;
    }*/

    @Override
    public List<HoaDonFullDto> getDataSortInBill(String name, String updown) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getDataSortInBill(name, updown);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getDataSearchInBill(String name, String content) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getDataSearchInBill(name, content);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getDateSearchInBill(String name, String dateBegin, String dateEnd) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getDateSearchInBill(name, dateBegin, dateEnd);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getFinishTimeSearchInBill(String name, String finish, String time, String dateBegin, String dateEnd) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getFinishTimeSearchInBill(name, finish, time, dateBegin, dateEnd);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getStatusTimeSearchInBill(String name, String status, String time, String dateBegin, String dateEnd) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getStatusTimeSearchInBill(name, status, time, dateBegin, dateEnd);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getFinish_StatusTimeSearchInBill(String finish, String status, String time, String dateBegin, String dateEnd) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getFinish_StatusTimeSearchInBill(finish, status, time, dateBegin, dateEnd);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getFinishSearchInBill(String name, String finish) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getFinishSearchInBill(name, finish);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getStatusSearchInBill(String name, String status) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getStatusSearchInBill(name, status);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    public List<HoaDonFullDto> getFinish_StatusSearchInBill(String finish, String status) {
        List<HoaDonFullDto> hoaDonDto = new ArrayList<HoaDonFullDto>();
        List<HoaDonFull> hoaDon = hoaDonDao.getFinish_StatusSearchInBill(finish, status);
        hoaDonDto = mapper.mapCollection(hoaDon, HoaDonFullDto.class);
        return hoaDonDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<ChiTietHoaDonDto> getBillDetailInfoDelete(List<ChiTietHoaDonDto> listBill){
        for(int i = 0; i < listBill.size(); i++) {
            chiTietHoaDonDao.delete(listBill.get(i).getMaHD());
        }
        return mapper.mapCollection(chiTietHoaDonDao.getBillDetailCommon(), ChiTietHoaDonDto.class);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public HoaDonFullDto updateStatusBill(String id, Long status) {
        HoaDonFull hoaDonFull = hoaDonFullDao.findById(id);
        if (hoaDonFull.getThanhToan() == ISSConstants.STATUS_PAYMENT_OK && status == ISSConstants.STATUS_BILL_3) {
            return null;
        }
        else {
            Date now = new Date();
//                dang giao hang
            if(status == ISSConstants.STATUS_BILL_0) {
                if(hoaDonFull.getNgayGiao() != null) {
                    hoaDonFull.setNgayGiao(null);
                }
                if(hoaDonFull.getNgayNhan() != null) {
                    hoaDonFull.setNgayNhan(null);
                }
            }

            if(status == ISSConstants.STATUS_BILL_1) {
                if(hoaDonFull.getNgayNhan() != null) {
                    hoaDonFull.setNgayNhan(null);
                }
                hoaDonFull.setNgayGiao(now);
            }
//                da giao hang
            if(status == ISSConstants.STATUS_BILL_2) {
                if(hoaDonFull.getTrangThai() == ISSConstants.STATUS_BILL_0) {
                    hoaDonFull.setNgayGiao(now);
                    hoaDonFull.setNgayNhan(now);
                }
                if(hoaDonFull.getTrangThai() == ISSConstants.STATUS_BILL_1) {
                    hoaDonFull.setNgayNhan(now);
                }
                hoaDonFull.setThanhToan(ISSConstants.STATUS_PAYMENT_OK);
            }
//              huy
            if(status == ISSConstants.STATUS_BILL_3) {
                hoaDonFull.setNgayGiao(null);
                hoaDonFull.setNgayNhan(null);
                removeBill(hoaDonFull);
            }
            hoaDonFull.setTrangThai(status);
            hoaDonFullDao.update(hoaDonFull);
        }
        HoaDonFullDto hoaDonFullDto = mapper.map(hoaDonFull, HoaDonFullDto.class, HOA_DON_FULL_MAP_ID);
        return hoaDonFullDto;
    }

    private void removeBill(HoaDonFull hoaDonFull) {
        List<ChiTietHoaDonFull> chiTietHoaDonFulls = hoaDonFull.getChiTietHoaDon();
        for(int i = 0; i < chiTietHoaDonFulls.size(); i++) {
            ChiTietSanPhamFull chiTietSanPhamFull = chiTietHoaDonFulls.get(i).getChiTietSanPham();
            ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(chiTietSanPhamFull.getMaCTSP());
            chiTietSanPham.setSoLuong(chiTietSanPham.getSoLuong() + chiTietHoaDonFulls.get(i).getSoLuong());
            chiTietSanPhamDao.update(chiTietSanPham);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public HoaDonFullDto updatePaymentBill(String id, Long payment) {
        HoaDonFull hoaDonFull = hoaDonFullDao.findById(id);
        hoaDonFull.setThanhToan(payment);
        hoaDonFullDao.update(hoaDonFull);
        HoaDonFullDto hoaDonFullDto = mapper.map(hoaDonFull, HoaDonFullDto.class, HOA_DON_FULL_MAP_ID);
        return hoaDonFullDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer deleteBill(List<HoaDonDto> hoaDonDtos) {
        try{
            for(int i = 0; i < hoaDonDtos.size(); i++) {
                chiTietHoaDonDao.deleteBill(hoaDonDtos.get(i).getMaHD());
                hoaDonDao.delete(hoaDonDtos.get(i).getMaHD());
            }
            return JsonResponseCode.SUCCESS;
        }
        catch (Exception ex) {
            return JsonResponseCode.FAILED;
        }
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)
//    public List<ChiTietHoaDonDto> getBillDetailInfoDelete(List<ChiTietHoaDonDto> listBill){
//        List<ChiTietHoaDonDto> chiTietHoaDonDto = new ArrayList<ChiTietHoaDonDto>();
////        for(int i = 0; i < listBill.size(); i++) {
//            List<ChiTietHoaDon> chiTietHoaDon = chiTietHoaDonDao.getBillDetailInfoDelete(listBill);
////        }
//        chiTietHoaDonDto = mapper.mapCollection(chiTietHoaDon, ChiTietHoaDonDto.class,CHI_TIET_HOA_DON_MAP_ID);
//        return chiTietHoaDonDto;
//    }
}

